<?php
/**
 * Created by PhpStorm.
 * User: 吴登科
 * Date: 2018/11/09
 * Time: 14:42
 * 我的
 */

namespace Home\Controller;

use Think\Controller;
use think\Session;

class SuggestController extends Controller
{
    public $customer_id = '';
    public $store_id = 0;

    /*
     * 初始化
     */
    public function _initialize()
    {
        if (session('?customer_id') && session('?STORE_ID')) {
            $this->customer_id = session('customer_id');//用户id
            $this->store_id = session('STORE_ID');//店铺id
        }
        $this->customer_id = "20181102115031877722";
        $this->store_id = '11';
    }

    /**
     * 建议反馈页面
     */
    public function index(){
        $this->display();
    }

    /**
     * 提交建议
     */
    public function suggest(){

        if ($this->store_id && $this->customer_id) {
            $data['suggestion'] = I('post.suggestion');
            $data['time'] = time();
            $data['store_id'] = $this->store_id;
            $data['type_name'] = I('post.type_name');
            $data['customer_id'] = $this->customer_id;
            $data['pic'] = I('post.pic');

            //判断用户是否之前增加过
            $suggestion = M('suggest')->where("customer_id= $this->customer_id and store_id = $this->store_id")->find();
            if($suggestion){
                $this->ajaxreturn(array('state' => -1,'data' => [],'msg'=>'请不要重复添加建议！'), 'json');
            }else{
                $result = M('suggest')->add($data);

                if($result !== false){
                    $this->ajaxreturn(array('state' => 1, 'data' => $data,'msg'=>'增加建议成功！'), 'json');
                }else{
                    $this->ajaxreturn(array('state' => 0,'data' => [],'msg'=>'添加建议失败！'), 'json');
                }
            }

        }else{
            $this->error("服务器繁忙请稍后再试！", U('Home/Index/index'));
        }
    }
}